package com.lushu.data
{
	import com.lushu.data.interfaces.ITreeProvider;
	import com.lushu.events.ResourceEvent;
	import com.lushu.helpers.io.GenericIO;
	import com.lushu.info.ApplicationData;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	import parts.renderer.ChargementBox;
	
	public class TilesetImage extends EventDispatcher implements ITreeProvider
	{
		private var name_ : String;
		private var ioUtil_ : GenericIO;
		private var ioUtilData_ : GenericIO;
		
		private var tilesetImage_ : BitmapData;
		private var box_ : ChargementBox;
		private var _tilesList : Vector.<RectangleID>;
		
		[Bindable]
		public var tilesPiece : ArrayCollection // of TilePiece;
		
		public var animationData : Dictionary;
		
		public function TilesetImage(tilesetName : String, box : ChargementBox, pathInfo : String)
		{
			box_ = box;
			name_ = tilesetName;
			if(name_.split(".").length == 1)
				ioUtil_ = new GenericIO(ApplicationData.BASE_LOCATION+"/"+pathInfo+name_+".png");
			else
				ioUtil_ = new GenericIO(ApplicationData.BASE_LOCATION+"/"+pathInfo+name_);
			ioUtil_.addEventListener(ResourceEvent.FILE_LOADED, parseTilesetData);
			ioUtil_.read(GenericIO.BITMAP_FILE);
			
			if(name_.split(".").length == 1)
				ioUtilData_ = new GenericIO(ApplicationData.BASE_LOCATION+"/"+pathInfo+name_+".data");
			else
				ioUtilData_ = new GenericIO(ApplicationData.BASE_LOCATION+"/"+pathInfo+name_.substr(0,name_.length - 4)+".data");
			ioUtilData_.addEventListener(ResourceEvent.FILE_LOADED, parseTilesData);
			ioUtilData_.read(GenericIO.XML_FILE);
			
			animationData = new Dictionary();
		}
			
		private function parseTilesetData(event : ResourceEvent) : void
		{
			tilesetImage_ = ioUtil_.getFileDataAsBitmap().bitmapData;
			box_.setSubLabel("Tileset : "+name_);
			dispatchEvent(new Event("tilesetLoaded"));	
		}
		
		private function parseTilesData(event : ResourceEvent) : void
		{
			var tilesData : XML = ioUtilData_.getFileData();
			_tilesList = new Vector.<RectangleID>();
			var tiles : XMLList = tilesData..tile;
			var tile : XML;
			var tileRect : Rectangle;
			var maxTile : int = -1;
			for each(tile in tiles)
			{
				var tileId : int;
				var tileIdStr : String = tile.@id;
				if(tileIdStr == "" || tileIdStr == null)
					tileId = -1
				else
					tileId = tile.@id;
				if(tileId == -1)
					tileId = maxTile + 1;
				maxTile = Math.max(tileId, maxTile);
				tileRect = new RectangleID(tile.@x, tile.@y, tile.@width, tile.@height, tileId);
				_tilesList.push(tileRect);
			}
			
			var animTile : XML;
			var actionStr : String;
			var faceStr : String;
			var animationTileList : Vector.<AnimationTilePiece>;
			var animationTilePiece : AnimationTilePiece;
			var animTileTile : XML;
			for each(animTile in tilesData..animation)
			{
				actionStr = animTile.@action;
				faceStr = animTile.@face;
				animationTileList = new Vector.<AnimationTilePiece>();
				for each(animTileTile in animTile..tileAnimation)
				{
					animationTilePiece = new AnimationTilePiece();
					animationTilePiece.id = animTileTile.@id;
					animationTilePiece.duration = animTileTile.@duration;
					animationTilePiece.xOffset = animTileTile.@xOffset;
					animationTilePiece.yOffset = animTileTile.@yOffset;
					if(animTileTile.@reversed == "true")
						animationTilePiece.reversed = true;
					else
						animationTilePiece.reversed = false;
					animationTileList.push(animationTilePiece);
				}
				animationData[faceStr+"-"+actionStr] = animationTileList;
			}
			dispatchEvent(new Event("tilesetLoaded"));	
		}
		
		public function getData() : BitmapData
		{
			return tilesetImage_;
		}
		
		public function getTileData() : Vector.<RectangleID>
		{
			return _tilesList;
		}
		
		public function getAnimationData(currentFace : String, currentAction : String) : Vector.<AnimationTilePiece>
		{
			return animationData[currentFace+"-"+currentAction] as Vector.<AnimationTilePiece>;
		}
		
		public function updateAnimationData(currentFace : String, currentAction : String, data : ArrayCollection) : void
		{
			var newAnimVector : Vector.<AnimationTilePiece> = new Vector.<AnimationTilePiece>();
			var animTilePiece : AnimationTilePiece;
			for each(animTilePiece in data)
			{
				newAnimVector.push(animTilePiece);
			}
			animationData[currentFace+"-"+currentAction] = newAnimVector;
		}
		public function getName() : String
		{
			return name_;
		}
		
		public function get label() : String
		{
			return name_;	
		}
	}
}